package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.mapper.IClientMapper;
import com.project.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClientServiceImpl implements IClientService {
    @Autowired
    private IClientMapper mapper;

    @Override
    public IPage<ClientBean> cutByItem(Integer pageNO, String cName, String cRange) {
        QueryWrapper<ClientBean> qw = new QueryWrapper<>();
        if (cName != null && cName.length() != 0) {
            qw.like("c_cName", cName);
        }
        if (cRange != null && cRange.length() != 0) {
            qw.like("c_cRange", cRange);
        }
        return mapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public ClientBean findByClientId(Integer clientId) {
        return mapper.selectById(clientId);
    }

    @Override
    public void updateClient(Integer clientId, String cLeader, String cLeaderPhone, String cRange) {
        ClientBean client = mapper.selectById(clientId);
        client.setcLeader(cLeader);
        client.setcLeaderPhone(cLeaderPhone);
        client.setcRange(cRange);
        mapper.updateById(client);
    }

    @Override
    public void addClient(ClientBean client) {
        mapper.insert(client);
    }

    @Override
    public List<ClientBean> findAllClient() {
        return mapper.selectList(null);
    }
}
